package org.buptsse.entity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.buptsse.utils.APPConstant;

import android.R.bool;
import android.provider.SyncStateContract.Constants;
import android.util.Log;

public class User implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int  id;
	private String Name;
	private String Mail;
	private String Sex;
	private int SecureScope;
	private int Fan;
	private String Password;
	private int Follow;
	private int PostAmount;
	private String Hometown;
	private int Age;
	private String image;
	private int flag;
	private boolean isLogin=false;
	
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	private boolean IsFollow;
	private String Birthday;
	private int relationId;
	public int getRelationId() {
		return relationId;
	}
	public void setRelationId(int relationId) {
		this.relationId = relationId;
	}
	public User()
	{
		
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getMail() {
		return Mail;
	}
	public void setMail(String mail) {
		Mail = mail;
	}
	public String getSex() {
		return Sex;
	}
	public void setSex(String sex) {
		Sex = sex;
	}
	public int getSecureScope() {
		return SecureScope;
	}
	public void setSecureScope(int secureScope) {
		SecureScope = secureScope;
	}
	public int getFan() {
		return Fan;
	}
	public void setFan(int fan) {
		Fan = fan;
	}
	public int getFollow() {
		return Follow;
	}
	public void setFollow(int follow) {
		Follow = follow;
	}
	public int getPostAmount() {
		return PostAmount;
	}
	public void setPostAmount(int postAmount) {
		PostAmount = postAmount;
	}
	public String getHometown() {
		return Hometown;
	}
	public void setHometown(String hometown) {
		Hometown = hometown;
	}
	public int getAge() {
		return Age;
	}
	public void setAge(int age) {
		Age = age;
	}
	public String getBirthday() {
		return Birthday;
	}
	public void setBirthday(String birthday) {
		Birthday = birthday;
	}
	public String getPassword() {
		return Password;
	}
	public void setPassword(String password) {
		Password = password;
	}
	public boolean isIsFollow() {
		return IsFollow;
	}
	public void setIsFollow(boolean isFollow) {
		IsFollow = isFollow;
	}
	
	public boolean isLogin()
	{
		return isLogin;
	}
	
	public void setLoginState(boolean isLogin)
	{
		this.isLogin=isLogin;
	}
	
	private String phoneNumber,school,firstSubject,secondSubject,degree,coin,photo;
	
	
	public String getPhoto() {
		return photo;
	}
	public void setPhoto(String photo) {
		this.photo = photo;
	}
	public String getCoin() {
		return coin;
	}
	public void setCoin(String coin) {
		this.coin = coin;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public String getSchool() {
		return school;
	}
	public void setSchool(String school) {
		this.school = school;
	}
	public String getFirstSubject() {
		return firstSubject;
	}
	public void setFirstSubject(String firstSubject) {
		this.firstSubject = firstSubject;
	}
	public String getSecondSubject() {
		return secondSubject;
	}
	public void setSecondSubject(String secondSubject) {
		this.secondSubject = secondSubject;
	}
	public String getDegree() {
		return degree;
	}
	public void setDegree(String degree) {
		this.degree = degree;
	}
	public String login(String name, String pwd)
	{
		HttpClient httpClient=new DefaultHttpClient();
		HttpPost httpPost=new HttpPost(APPConstant.LoginUrl);
		ArrayList<NameValuePair> param=new ArrayList<NameValuePair>();
		try {
			param.add(new BasicNameValuePair("username", name));
			param.add(new BasicNameValuePair("password", pwd));
			httpPost.setEntity(new UrlEncodedFormEntity(param,"utf-8"));
			HttpResponse response=httpClient.execute(httpPost);
			HttpEntity entity=response.getEntity();
			InputStream content=entity.getContent();
			String result=convertStreamToString(content);
			if(!result.equals("failure"))
			{
				this.Name=name;
				this.Password=pwd;
				this.isLogin=true;
			}
			return result;
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private String convertStreamToString(InputStream is) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));

		StringBuilder sb = new StringBuilder();

		String line = null;

		try {

			while ((line = reader.readLine()) != null) {

				sb.append(line);

			}

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				is.close();

			} catch (IOException e) {

				e.printStackTrace();

			}

		}

		return sb.toString();

	}

	
}
